package com.cy.lib.redis.core.serializer;

import java.io.IOException;

import org.apache.commons.pool2.impl.GenericObjectPool;
import org.msgpack.MessagePack;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

public class MsgPackRedisSerializer<T> implements RedisSerializer<T> {

	private MessagePack msgPack;

	private GenericObjectPool<MessagePack> pool;

	public void setPool(GenericObjectPool<MessagePack> pool) {
		this.pool = pool;
	}

	public byte[] serialize(T t) throws SerializationException {
		if(t==null)
			return new byte[0];
		try {
			msgPack = pool.borrowObject();
		} catch (Exception e1) {
			msgPack = new MessagePack();
		}
		try {
			return msgPack.write(t);
		} catch (IOException e) {
			e.printStackTrace();
			return new byte[0];
		}
	}

	public T deserialize(byte[] bytes) throws SerializationException {
		try {
			msgPack = pool.borrowObject();
		} catch (Exception e1) {
			msgPack = new MessagePack();
		}
		try {
			T t = (T)msgPack.read(bytes);
			pool.returnObject(msgPack);
			return t;
		} catch (IOException e) {
			e.printStackTrace();
			pool.returnObject(msgPack);
			return null;
		}
	}

}
